home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / 42 ƒ / 42.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  6.8 KB  |  361 lines  |  [TEXT/CWIE]

  1. /*
  2.     ThreadedAppShell.c
  3.     copyright (c)1999 Ben Martz
  4. */
  5.  
  6. /* includes */
  7. #include <Threads.h>
  8. #include "malloc.h"
  9. #include "42.h"
  10. #include "42_plugin_manager.h"
  11. #include "42_plugin_life.h"
  12. #include "42_plugin_ball.h"
  13.  
  14. /* resource ids */
  15. #define    kMenuBarID    128
  16. #define        mApple    128
  17. #define            iAbout    1
  18. #define        mFile    129
  19. #define            iQuit    1
  20. #define        mEdit    130
  21. #define    kWindowID    128
  22. #define    kAboutID    128
  23. #define    kErrorID    129
  24.  
  25. /* prototypes */
  26. void InitToolbox(void);
  27. void InitEnvironment(void);
  28. void InitThread(void *entry);
  29. void EventThread(void);
  30. int HandleDialogEvent(EventRecord *event);
  31. void HandleMouseDownEvent(EventRecord *event);
  32. void HandleUpdateEvent(EventRecord *event);
  33. void HandleMenuChoice(long choice);
  34. void HandleAEQuit(void);
  35. void ExitError(unsigned char *msg);
  36. void SetTextBox(short item, unsigned char *str);
  37. void CheckTextBox(short item);
  38. void SetControlFont(short item);
  39.  
  40. /* globals */
  41. Boolean        gDone = false;
  42. DialogPtr    gMainDialog;
  43.  
  44. /* main entry */
  45. int main(void)
  46. {
  47.     InitToolbox();
  48.     InitEnvironment();
  49.     InitThread(EventThread);
  50.     InitThread(forty_two_thread);
  51.  
  52.     while(!gDone)
  53.         YieldToAnyThread();
  54.  
  55.     return 0;
  56. } /* main */
  57.  
  58. /* the macintosh toolbox is our friend! */
  59. void InitToolbox(void)
  60. {
  61.     InitGraf(&qd.thePort);
  62.     InitFonts();
  63.     FlushEvents(everyEvent,0L);
  64.     InitWindows();
  65.     InitMenus();
  66.     TEInit();
  67.     InitDialogs(nil);
  68.     InitCursor();
  69.  
  70.     MaxApplZone();
  71.     MoreMasters();
  72. } /* InitToolbox */
  73.  
  74. /* set up our little world that we live in */
  75. void InitEnvironment(void)
  76. {
  77.     Handle        mbar;
  78.     MenuHandle    applemenu;
  79.     int            i;
  80.     
  81.     mbar = GetNewMBar(kMenuBarID);
  82.     if(!mbar)
  83.         ExitError("\pCan't load menu bar!");
  84.     SetMenuBar(mbar);
  85.     
  86.     applemenu = GetMenuHandle(mApple);
  87.     AppendResMenu(applemenu,'DRVR');
  88.     InsertMenu(applemenu, 0);
  89.     
  90.     DrawMenuBar();
  91.     
  92.     gMainDialog = GetNewDialog(130,0,(WindowPtr)-1L);
  93.     if(!gMainDialog)
  94.         ExitError("\pcan't load dialog");
  95.  
  96.     SetPort(gMainDialog);
  97.     TextFont(1);
  98.     TextSize(9);
  99.     for(i = 0; i < last_item; i++)
  100.         SetControlFont(i);
  101.     ShowWindow(gMainDialog);
  102.     
  103.     DrawDialog(gMainDialog);
  104.     
  105.     AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,(AEEventHandlerUPP)HandleAEQuit,0,false);
  106. } /* InitEnvironment */
  107.  
  108. /* set up a new thread in our world */
  109. void InitThread(void *entry)
  110. {
  111.     if(NewThread(kCooperativeThread,(ThreadEntryProcPtr)entry,nil,0,kCreateIfNeeded,nil,nil))
  112.         ExitError("\pUnable to create thread!");
  113. } /* InitThread */
  114.  
  115. /* main event dispatch */
  116. void EventThread(void)
  117. {
  118.     EventRecord    event;
  119.     
  120.     while(!gDone)
  121.     {
  122.         if(WaitNextEvent(everyEvent,&event,0,nil))
  123.         {
  124.             if(IsDialogEvent(&event))
  125.             {
  126.                 if(HandleDialogEvent(&event)) continue;
  127.             }
  128.             switch(event.what) {
  129.                 case keyDown:
  130.                     if(event.modifiers & cmdKey)
  131.                         HandleMenuChoice(MenuKey(event.message & charCodeMask));
  132.                     break;
  133.                 case autoKey:
  134.                     break;
  135.                 case mouseDown:
  136.                     HandleMouseDownEvent(&event);
  137.                     break;
  138.                 case activateEvt:
  139.                 case updateEvt:
  140.                     HandleUpdateEvent(&event);
  141.                     break;
  142.                 case kHighLevelEvent:
  143.                     AEProcessAppleEvent(&event);
  144.                     break;
  145.             } /* switch */
  146.         } /* if */
  147.  
  148.         YieldToAnyThread();
  149.     } /* while */
  150. } /* EventThread */
  151.  
  152. int HandleDialogEvent(EventRecord *event)
  153. {
  154.     DialogPtr    dialog;
  155.     short        item;
  156.     
  157.     if(event->modifiers & cmdKey)
  158.     {
  159.         HandleMenuChoice(MenuKey(event->message & charCodeMask));
  160.         return 1;
  161.     }
  162.     
  163.     if(FrontWindow() != gMainDialog) return 0;
  164.     
  165.     if(DialogSelect(event, &dialog, &item))
  166.     {
  167.         switch(item)
  168.         {
  169.             case start:
  170.                 life_ipc(LIFE_START,0);
  171.                 break;
  172.             case stop:
  173.                 life_ipc(LIFE_STOP,0);
  174.                 break;
  175.             case step:
  176.                 life_ipc(LIFE_STEP,0);
  177.                 break;
  178.             case random:
  179.                 life_ipc(LIFE_LOAD,5);
  180.                 break;
  181.             case pattern_1:
  182.             case pattern_2:
  183.             case pattern_3:
  184.             case pattern_4:
  185.                 life_ipc(LIFE_LOAD,item - pattern_1 + 1);
  186.                 break;
  187.             case color_c:
  188.                 life_ipc(LIFE_SETCOLOR,0x0000FFFF);
  189.                 ball_ipc(BALL_COLOR,0x00FFFF00);
  190.                 break;
  191.             case color_y:
  192.                 life_ipc(LIFE_SETCOLOR,0x00FFFF00);
  193.                 ball_ipc(BALL_COLOR,0x00FF00FF);
  194.                 break;
  195.             case color_m:
  196.                 life_ipc(LIFE_SETCOLOR,0x00FF00FF);
  197.                 ball_ipc(BALL_COLOR,0x00000000);
  198.                 break;
  199.             case color_k:
  200.                 life_ipc(LIFE_SETCOLOR,0x00000000);
  201.                 ball_ipc(BALL_COLOR,0x0000FFFF);
  202.                 break;
  203.             case ticker_buy:
  204.                 SysBeep(10);
  205.                 break;
  206.             default:
  207.                 GlobalToLocal(&event->where);
  208.                 forty_two_click(&event->where);
  209.                 break;
  210.         }
  211.         return 1;
  212.     }
  213.     else
  214.         return 0;
  215. } /* HandleDialogEvent */
  216.  
  217. /* handle mousedown events */
  218. void HandleMouseDownEvent(EventRecord *event)
  219. {
  220.     WindowPtr    wind;
  221.     short        part;
  222.     Rect        rect = {-4096,-4096,4096,4096};
  223.     
  224.     part = FindWindow(event->where, &wind);
  225.     
  226.     switch(part)
  227.     {
  228.         case inMenuBar:
  229.             HandleMenuChoice(MenuSelect(event->where));
  230.             break;
  231.         case inSysWindow:
  232.             SystemClick(event, wind);
  233.         case inContent:
  234.             if(wind != FrontWindow())
  235.                 SelectWindow(wind);
  236.             if(wind == gMainDialog)
  237.             {
  238.                 GlobalToLocal(&event->where);
  239.                 forty_two_click(&event->where);
  240.             }
  241.             break;
  242.         case inDrag:
  243.             DragWindow(wind, event->where,&rect);
  244.             break;
  245.         case inGoAway:
  246.             if(TrackGoAway(wind,event->where))
  247.                 gDone = true;
  248.             break;
  249.     } /* switch */
  250. } /* HandleMouseDownEvent */
  251.  
  252. /* handle window updates */
  253. void HandleUpdateEvent(EventRecord *event)
  254. {
  255.     WindowPtr    wind;
  256.     RgnHandle    clip,oldclip;
  257.     
  258.     wind = (WindowPtr) event->message;
  259.     
  260.     BeginUpdate(wind);
  261.     DrawDialog(gMainDialog);
  262.     EndUpdate(wind);
  263.     
  264.     DrawDialog(gMainDialog);
  265.     forty_two_update_all();
  266. } /* HandleUpdateEvent */
  267.  
  268. /* handle a menu selection or command key */
  269. void HandleMenuChoice(long choice)
  270. {
  271.     short        menu = HiWord(choice);
  272.     short        item = LoWord(choice);
  273.     MenuHandle    applemenu;
  274.     Str255        accname;
  275.     
  276.     switch(menu)
  277.     {
  278.         case mApple:
  279.             switch(item)
  280.             {
  281.                 case iAbout:
  282.                     Alert(kAboutID,nil);
  283.                     break;
  284.                 default:
  285.                     applemenu = GetMenuHandle(mApple);
  286.                     GetMenuItemText(applemenu,item,accname);
  287.                     OpenDeskAcc(accname);
  288.             } /* switch */
  289.             break;
  290.         case mFile:
  291.             switch(item)
  292.             {
  293.                 case iQuit:
  294.                     gDone = true;
  295.                     break;
  296.             } /* switch */
  297.             break;
  298.         case mEdit:
  299.             break;
  300.     } /* switch */
  301.     
  302.     HiliteMenu(0);
  303. } /* HandleMenuChoice */
  304.  
  305. /* we are a peaceful application */
  306. void HandleAEQuit(void)
  307. {
  308.     gDone = true;
  309. } /* HandleAEQuit */
  310.  
  311. /* display an error dialog and quit */
  312. void ExitError(unsigned char *msg)
  313. {
  314.     ParamText(msg,"\p","\p","\p");
  315.     Alert(kErrorID,nil);
  316.     ExitToShell();
  317. } /* ExitError */
  318.  
  319. void SetTextBox(short item, unsigned char *str)
  320. {
  321.     short            t;
  322.     Handle            h;
  323.     Rect            r;
  324.     
  325.     GetDialogItem(gMainDialog,item,&t,&h,&r);
  326.     if(h)
  327.     {
  328.         SetDialogItemText(h,str);
  329.     }
  330. }
  331.  
  332. void CheckTextBox(short item)
  333. {
  334.     short            t;
  335.     Handle            h;
  336.     Rect            r;
  337.     Str255            str;
  338.     
  339.     GetDialogItem(gMainDialog,item,&t,&h,&r);
  340.     if(h)
  341.     {
  342.         GetDialogItemText(h,str);
  343.         if(str[0] > 7) str[0] = 7;
  344.         SetDialogItemText(h,str);
  345.     }
  346. }
  347.  
  348. void SetControlFont(short item)
  349. {
  350.     short                    t;
  351.     Handle                    h;
  352.     Rect                    r;
  353.     ControlFontStyleRec        style;
  354.     
  355.     GetDialogItem(gMainDialog,item,&t,&h,&r);
  356.     style.flags = kControlUseFontMask | kControlUseSizeMask | kControlUseJustMask;
  357.     style.font = 1;
  358.     style.size = 9;
  359.     style.just = 1;
  360.     SetControlFontStyle((ControlHandle)h,&style);
  361. }